package net.dirox.confluence.plugin.servlet;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.dirox.confluence.plugin.ao.entities.Note;
import net.dirox.confluence.plugin.ao.entities.User;
import net.dirox.confluence.plugin.dao.service.NoteService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.atlassian.user.UserManager;

public class ProcessServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger log = LoggerFactory
			.getLogger(ProcessServlet.class);

	private final NoteService noteService;
	private UserManager userManager;

	public ProcessServlet(NoteService noteService, UserManager userManager) {
		this.noteService = checkNotNull(noteService);
		this.userManager = checkNotNull(userManager);
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (request.getParameter("action") != null) {
			String action = request.getParameter("action");
			User currentUser = noteService.currentUser();

			if (action.equals("Create")) {
				if (request.getParameter("content") != null
						&& request.getParameter("content").equals("") == false) {
					String content = request.getParameter("content");
					String priority = request.getParameter("priority");
					Note note = noteService.add(content,
							Integer.parseInt(priority));
					// if (request.getParameter("noteid") == null
					// || request.getParameter("noteid").length() == 0) {
					// if (request.getParameter("user") != null
					// && request.getParameter("user").equals("-1") == false) {
					// String userName = request.getParameter("user");
					// com.atlassian.crowd.embedded.api.User user =
					// jiraUserManager
					// .getUser(userName);
					// noteService.tagNoteToUser(note, user);
					// }
					response.sendRedirect(request.getContextPath()
							+ "/users/viewmynotes.action");
				}
			}
		}
	}

}